package com.hundred.home.module.screen;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.util.StringUtil;
import com.hundred.item.dao.dataobject.ItemDO;
import com.hundred.item.manager.ItemCompareManager;
import com.hundred.item.manager.ItemManager;
import com.hundred.item.manager.TopItemManager;

public class WebIndex {

	private static final Logger logger = LoggerFactory.getLogger(Index.class);

	@Autowired
    private HttpServletRequest request;
	@Autowired
	private ItemManager itemManager;
	@Autowired
	private TopItemManager topItemManager;
	@Autowired
	private ItemCompareManager itemCompareManager;

	public void execute(Context context, Navigator nav) {

		String command = request.getParameter("syn");
		if(command!=null && "true".equals(command)){
			logger.error("---syn---templetes--");
			synTemplete();
		}
		
		String tagStr = request.getParameter("tag");
		int tag = 100;
		if(!StringUtil.isBlank(tagStr)){
			tag=Integer.parseInt(tagStr);
		}
		List<ItemDO> itemList = itemCompareManager.selectCompareItems(2,tag);
		
		context.put("leftItem", itemList.get(0));
		
		context.put("rightItem", itemList.get(1));
		
		
		List<ItemDO> topItems = topItemManager.getTopItemList(10L);
	
		if(topItems!=null && topItems.size()>0){

			context.put("topItem", topItems.get(0));
			context.put("topItemList",topItems);
			
		}
		
		context.put("result", "ok");
		context.put("closeTime", getCloserTime());

		
	}
	
	private String getCloserTime(){
		
		Calendar cpcalendar = new GregorianCalendar();
		long nowTime=cpcalendar.getTimeInMillis();
        cpcalendar.add(Calendar.DAY_OF_MONTH,1);
        cpcalendar.set(Calendar.HOUR_OF_DAY, 0);
        cpcalendar.set(Calendar.MINUTE, 0);
        cpcalendar.set(Calendar.SECOND, 0);
        long  targetTime=cpcalendar.getTimeInMillis();
        int expireTime=(int) ((targetTime-nowTime)/1000);
        
        int hours = expireTime/3600;
        
        int minute = (expireTime%3600)/60;
        
        String closeTime = String.valueOf(hours)+"小时"+String.valueOf(minute)+"分";
        return closeTime;
	}
	
	public ItemManager getItemManager() {
		return itemManager;
	}

	public void setItemManager(ItemManager itemManager) {
		this.itemManager = itemManager;
	}

	public TopItemManager getTopItemManager() {
		return topItemManager;
	}

	public void setTopItemManager(TopItemManager topItemManager) {
		this.topItemManager = topItemManager;
	}

	public ItemCompareManager getItemCompareManager() {
		return itemCompareManager;
	}

	public void setItemCompareManager(ItemCompareManager itemCompareManager) {
		this.itemCompareManager = itemCompareManager;
	}

	private void synTemplete(){
		
		Process process=null;
		try {
			process = Runtime.getRuntime().exec("/home/admin/script/syntemplete.sh");
			process.waitFor();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	    if (process != null) {
	        process.destroy();
	    }
	}
    

}
